package com.hua.yuan.view;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.hua.yuan.R;
import com.hua.yuan.base.Constents;
import com.hua.yuan.ui.ImageDetailActivity;
import com.hua.yuan.ui.ImageShowActivity;
import com.hua.yuan.ui.data.PictureDataRequest;
import com.hua.yuan.ui.module.ImageItem;
import com.hua.yuan.util.CheckLoginUtil;
import com.zhy.http.okhttp.callback.StringCallback;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import cm.hua.yuan.common.utils.PreferenceUtils;
import okhttp3.Call;

/**
 * Created by huangyang on 2018/7/15.
 * 图片列表的item
 */

public class ImageItemView extends RelativeLayout {

    private ImageView imageTip1;
    private LinearLayout imageMoreTipLl;

    private ImageView image_image_1;
    private TextView redu1;
    private TextView money1;
    private TextView imageNum1;
    private TextView imageName1;
    private RelativeLayout goumaiRl1;
    private TextView money1_1;
    private TextView money1_2;
    private TextView goumai1;


    private ImageView image_image_2;
    private TextView redu2;
    private TextView money2;
    private TextView imageNum2;
    private TextView imageName2;
    private RelativeLayout goumaiRl2;
    private TextView money2_1;
    private TextView money2_2;
    private TextView goumai2;


    private ImageView image_image_3;
    private TextView redu3;
    private TextView money3;
    private TextView imageNum3;
    private TextView imageName3;
    private RelativeLayout goumaiRl3;
    private TextView money3_1;
    private TextView money3_2;
    private TextView goumai3;

    private ImageView image_image_4;
    private TextView redu4;
    private TextView money4;
    private TextView imageNum4;
    private TextView imageName4;
    private RelativeLayout goumaiRl4;
    private TextView money4_1;
    private TextView money4_2;
    private TextView goumai4;

    private ImageView image_image_5;
    private TextView redu5;
    private TextView money5;
    private TextView imageNum5;
    private TextView imageName5;
    private RelativeLayout goumaiRl5;
    private TextView money5_1;
    private TextView money5_2;
    private TextView goumai5;


    private ImageView image_image_6;
    private TextView redu6;
    private TextView money6;
    private TextView imageNum6;
    private TextView imageName6;
    private RelativeLayout goumaiRl6;
    private TextView money6_1;
    private TextView money6_2;
    private TextView goumai6;

    private LinearLayout video_fresh_ll;
    private ImageView video_fresh_image;

    private Context context;
    private boolean flag = true;
    private int type = 0;

    private void initView(Context context) {
        this.context = context;
        LayoutInflater.from(context).inflate(R.layout.image_item_view, this);

        imageTip1 = findViewById(R.id.imageTip1);
        imageMoreTipLl = findViewById(R.id.imageMoreTipLl);

        image_image_1 = findViewById(R.id.image_image_1);
        redu1 = findViewById(R.id.redu1);
        money1 = findViewById(R.id.money1);
        imageNum1 = findViewById(R.id.imageNum1);
        imageName1 = findViewById(R.id.imageName1);
        goumaiRl1 = findViewById(R.id.goumaiRl1);
        money1_1 = findViewById(R.id.money1_1);
        money1_2 = findViewById(R.id.money1_2);
        goumai1 = findViewById(R.id.goumai1);

        image_image_2 = findViewById(R.id.image_image_2);
        redu2 = findViewById(R.id.redu2);
        money2 = findViewById(R.id.money2);
        imageNum2 = findViewById(R.id.imageNum2);
        imageName2 = findViewById(R.id.imageName2);
        goumaiRl2 = findViewById(R.id.goumaiRl2);
        money2_1 = findViewById(R.id.money2_1);
        money2_2 = findViewById(R.id.money2_2);
        goumai2 = findViewById(R.id.goumai2);


        image_image_3 = findViewById(R.id.image_image_3);
        redu3 = findViewById(R.id.redu3);
        money3 = findViewById(R.id.money3);
        imageNum3 = findViewById(R.id.imageNum3);
        imageName3 = findViewById(R.id.imageName3);
        goumaiRl3 = findViewById(R.id.goumaiRl3);
        money3_1 = findViewById(R.id.money3_1);
        money3_2 = findViewById(R.id.money3_2);
        goumai3 = findViewById(R.id.goumai3);


        image_image_4 = findViewById(R.id.image_image_4);
        redu4 = findViewById(R.id.redu4);
        money4 = findViewById(R.id.money4);
        imageNum4 = findViewById(R.id.imageNum4);
        imageName4 = findViewById(R.id.imageName4);
        goumaiRl4 = findViewById(R.id.goumaiRl4);
        money4_1 = findViewById(R.id.money4_1);
        money4_2 = findViewById(R.id.money4_2);
        goumai4 = findViewById(R.id.goumai4);

        image_image_5 = findViewById(R.id.image_image_5);
        redu5 = findViewById(R.id.redu5);
        money5 = findViewById(R.id.money5);
        imageNum5 = findViewById(R.id.imageNum5);
        imageName5 = findViewById(R.id.imageName5);
        goumaiRl5 = findViewById(R.id.goumaiRl5);
        money5_1 = findViewById(R.id.money5_1);
        money5_2 = findViewById(R.id.money5_2);
        goumai5 = findViewById(R.id.goumai5);


        image_image_6 = findViewById(R.id.image_image_6);
        redu6 = findViewById(R.id.redu6);
        money6 = findViewById(R.id.money6);
        imageNum6 = findViewById(R.id.imageNum6);
        imageName6 = findViewById(R.id.imageName6);
        goumaiRl6 = findViewById(R.id.goumaiRl6);
        money6_1 = findViewById(R.id.money6_1);
        money6_2 = findViewById(R.id.money6_2);
        goumai6 = findViewById(R.id.goumai6);


        money1_1.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
        money2_1.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
        money3_1.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
        money4_1.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
        money5_1.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
        money6_1.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);

        video_fresh_ll = findViewById(R.id.video_fresh_ll);
        video_fresh_image = findViewById(R.id.video_fresh_image);
    }


    public ImageItemView(Context context) {
        super(context);
        initView(context);
    }

    public ImageItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public ImageItemView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    @SuppressLint("NewApi")
    public ImageItemView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initView(context);
    }


    public void setData(List<ImageItem> list, final int type) {
        this.type = type;

        if (type == 0) {
            //火热推荐
            imageTip1.setImageResource(R.mipmap.video_fg_com_tv);

        } else if (type == 1) {
            //付费精品
            imageTip1.setImageResource(R.mipmap.video_fg_ff_tip);
            money1.setVisibility(VISIBLE);
            money2.setVisibility(VISIBLE);
            money3.setVisibility(VISIBLE);
            money4.setVisibility(VISIBLE);
            money5.setVisibility(VISIBLE);
            money6.setVisibility(VISIBLE);

        } else if (type == 2) {
            //包月精选
            imageTip1.setImageResource(R.mipmap.video_fg_by_tip);

        } else if (type == 3) {
            //69特价
            imageTip1.setImageResource(R.mipmap.video_fg_tj_img);

            goumaiRl1.setVisibility(VISIBLE);
            goumaiRl2.setVisibility(VISIBLE);
            goumaiRl3.setVisibility(VISIBLE);
            goumaiRl4.setVisibility(VISIBLE);
            goumaiRl5.setVisibility(VISIBLE);
            goumaiRl6.setVisibility(VISIBLE);
        }

        if (list != null && list.size() > 0) {
            showUi(list.get(0), image_image_1, redu1, money1, imageNum1, imageName1, money1_1, money1_2);
        }

        if (list != null && list.size() > 1) {
            showUi(list.get(1), image_image_2, redu2, money2, imageNum2, imageName2, money2_1, money2_2);
        }

        if (list != null && list.size() > 2) {
            showUi(list.get(2), image_image_3, redu3, money3, imageNum3, imageName3, money3_1, money3_2);
        }

        if (list != null && list.size() > 3) {
            showUi(list.get(3), image_image_4, redu4, money4, imageNum4, imageName4, money4_1, money4_2);
        }

        if (list != null && list.size() > 4) {
            showUi(list.get(4), image_image_5, redu5, money5, imageNum5, imageName5, money5_1, money5_2);
        }

        if (list != null && list.size() > 5) {
            showUi(list.get(5), image_image_6, redu6, money6, imageNum6, imageName6, money6_1, money6_2);
        }

        imageMoreTipLl.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                context.startActivity(new Intent(context, ImageShowActivity.class).putExtra("type", type));
            }
        });

        video_fresh_ll.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                video_fresh_image.startAnimation(AnimationUtils.loadAnimation(context, R.anim.rotate_refresh));
                freshData();
            }
        });

    }


    private int reduNum = 0;

    @SuppressLint("SetTextI18n")
    private void showUi(final ImageItem imageItem,
                        ImageView image,
                        final TextView redu,
                        TextView money,
                        TextView imageNum,
                        TextView imageName,
                        TextView money_1,
                        TextView money_2) {


        if (imageItem == null) {
            return;
        }

        //显示图片
        Glide.with(context)
                .load(imageItem.getCover_img_url())
                .into(image);

        //显示热度
        reduNum = imageItem.getReduNum();
        redu.setText("热度：" + reduNum);
        redu.setTag(reduNum);

        //显示代币
        money.setText(imageItem.getNow_price() + "代币");

        //显示张数
        imageNum.setText(imageItem.getPic_num() + "张");

        //显示图片的名字
        imageName.setText(imageItem.getTitle());

        //显示代币1
        money_1.setText(imageItem.getOrigin_price() + "代币");

        //显示代币2
        money_2.setText(imageItem.getNow_price() + "代币");

        image.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (CheckLoginUtil.check()) {
                    int reduNum = (Integer) redu.getTag() + 1;
                    redu.setText("热度：" + reduNum);
                    redu.setTag(reduNum);
                    context.startActivity(new Intent(context, ImageDetailActivity.class).putExtra("id", imageItem.getAtlas_id()));
                } else {
                    CheckLoginUtil.goToLogin((Activity) context);
                }
            }
        });

    }

    private int from = 0;

    /**
     * 换一批数据
     */
    private void freshData() {

        if (flag) {

            flag = false;
            String token = PreferenceUtils.getString(Constents.TOKEN, "");
//            final int from = PreferenceUtils.getInt(MainApplication.getInstance(), Constents.HOT_DATA_SATRT, 0);

            PictureDataRequest.getPictures(token, 1, from, 6, new StringCallback() {
                @Override
                public void onError(Call call, Exception e, int id) {
                    flag = true;
                }

                @Override
                public void onResponse(String response, int id) {

                    flag = true;

                    try {
                        JSONObject jsonObject = new JSONObject(response);

                        if (!jsonObject.isNull("errno")) {
                            String code = jsonObject.getString("errno");

                            if ("0".equals(code)) {

                                if (!jsonObject.isNull("data")) {

                                    JSONArray dataJson = jsonObject.getJSONArray("data");

                                    Gson gson = new Gson();

                                    List<ImageItem> list = new ArrayList<>();

                                    for (int i = 0; i < dataJson.length(); i++) {
                                        JSONObject data = dataJson.getJSONObject(i);
                                        ImageItem bean = gson.fromJson(data.toString(), ImageItem.class);
                                        list.add(bean);
                                    }

                                    if (list.size() >= 6) {
//                                        PreferenceUtils.setInt(MainApplication.getInstance(), Constents.HOT_DATA_SATRT, from + 6);
                                        from += 6;
                                        setData(list, type);
                                    }

                                    if (list.size() == 0) {
                                        from = 0;
//                                        PreferenceUtils.setInt(MainApplication.getInstance(), Constents.HOT_DATA_SATRT, 0);
                                    }

                                }

                            }

                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }


                }
            });


        }


    }

//    public void setData(ImageBean bean) {
//
//        final int type = bean.getType();
//
//        if (type == 0) {
//            //火热推荐
//            imageTip1.setImageResource(R.mipmap.video_fg_com_tv);
//
//
//        } else if (type == 1) {
//            //付费精品
//            imageTip1.setImageResource(R.mipmap.video_fg_ff_tip);
//            money1.setVisibility(VISIBLE);
//            money2.setVisibility(VISIBLE);
//            money3.setVisibility(VISIBLE);
//            money4.setVisibility(VISIBLE);
//            money5.setVisibility(VISIBLE);
//            money6.setVisibility(VISIBLE);
//
//
//        } else if (type == 2) {
//            //包月精选
//            imageTip1.setImageResource(R.mipmap.video_fg_by_tip);
//
//
//        } else if (type == 3) {
//            //69特价
//            imageTip1.setImageResource(R.mipmap.video_fg_tj_img);
//
//            goumaiRl1.setVisibility(VISIBLE);
//            goumaiRl2.setVisibility(VISIBLE);
//            goumaiRl3.setVisibility(VISIBLE);
//            goumaiRl4.setVisibility(VISIBLE);
//            goumaiRl5.setVisibility(VISIBLE);
//            goumaiRl6.setVisibility(VISIBLE);
//
//        }
//
//
//        List<ImageItem> list = bean.getList();
//        if (list != null && list.size() > 0) {
//            showUi(list.get(0), image_image_1, redu1, money1, imageNum1, imageName1, money1_1, money1_2);
//        }
//
//
//        if (list != null && list.size() > 1) {
//            showUi(list.get(1), image_image_2, redu2, money2, imageNum2, imageName2, money2_1, money2_2);
//        }
//
//
//        if (list != null && list.size() > 2) {
//            showUi(list.get(2), image_image_3, redu3, money3, imageNum3, imageName3, money3_1, money3_2);
//        }
//
//
//        if (list != null && list.size() > 3) {
//            showUi(list.get(3), image_image_4, redu4, money4, imageNum4, imageName4, money4_1, money4_2);
//        }
//
//
//        if (list != null && list.size() > 4) {
//            showUi(list.get(4), image_image_5, redu5, money5, imageNum5, imageName5, money5_1, money5_2);
//        }
//
//        if (list != null && list.size() > 5) {
//            showUi(list.get(5), image_image_6, redu6, money6, imageNum6, imageName6, money6_1, money6_2);
//        }
//
//        imageMoreTipLl.setOnClickListener(new OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                context.startActivity(new Intent(context, ImageShowActivity.class).putExtra("type", type));
//            }
//        });
//
//
//        if (type == 3) {
//            //69特价
//            goumai1.setOnClickListener(new OnClickListener() {
//                @Override
//                public void onClick(View view) {
//
//                }
//            });
//
//            goumai2.setOnClickListener(new OnClickListener() {
//                @Override
//                public void onClick(View view) {
//
//                }
//            });
//
//            goumai3.setOnClickListener(new OnClickListener() {
//                @Override
//                public void onClick(View view) {
//
//                }
//            });
//
//            goumai4.setOnClickListener(new OnClickListener() {
//                @Override
//                public void onClick(View view) {
//
//                }
//            });
//
//            goumai5.setOnClickListener(new OnClickListener() {
//                @Override
//                public void onClick(View view) {
//
//                }
//            });
//
//            goumai6.setOnClickListener(new OnClickListener() {
//                @Override
//                public void onClick(View view) {
//
//                }
//            });
//        }
//
//    }
}
